﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConceptionLogiciel_Devoir1
{
    public partial class ucLocation : UserControl
    {
        SessionLocation sessionLocation;
        String noPermis;
        String noVehicule;
        Facture paiement;

        public ucLocation()
        {
            InitializeComponent();
            sessionLocation = new SessionLocation();
            noPermis = "";
            noVehicule = "";
        }

        private void ucLocation_Load(object sender, EventArgs e)
        {
            loadLstClient();
            loadLstVehicule();
            btnNouveau.Enabled = false;
            btnEnregistrer.Enabled = false;
            btnSupprimer.Enabled = false;
            btnImprimerFacture.Enabled = false;
            cboEndroitRetour.Enabled = false;
            gprRechercheVehicule.Enabled = false;
            gprRechercheClient.Enabled = false;
            lblPaiement.Text = "Choisir un client et un véhicule dans les listes.";
            lblPaiement.ForeColor = Color.Black;
            dtpFin.MinDate = DateTime.Today.AddDays(1);
        }

        private void loadLstClient()
        {
            lstClient.Items.Clear();
            CatalogueClient catClient = CatalogueClient.getInstance();
            foreach (KeyValuePair<string, Client> item in catClient.getCatalogueClient())
            {
                ListViewItem item1 = new ListViewItem(new string[] { item.Value.Nom, item.Value.Prenom, item.Value.NoTelephone, 
                    item.Value.NoPermisConduire, item.Value.Adresse });
                lstClient.Items.Add(item1);
                item1.Tag = item.Value.NoPermisConduire;
            }
        }

        private void loadLstVehicule()
        {
            lstVehicule.Items.Clear();
            CatalogueVehicule catVehicule = CatalogueVehicule.getInstance();
            String vehDispo;
            foreach (KeyValuePair<string, Vehicule> item in catVehicule.getCatalogueVehicule())
            {
                if (item.Value.Disponibilite.getDisponibilite() == true)
                    vehDispo = "Oui";
                else
                    vehDispo = "Non";

                ListViewItem item1 = new ListViewItem(new string[] { item.Value.Marque, item.Value.Modele, 
                                                                     item.Value.Annee.ToString(), 
                    item.Value.NoSerie, item.Value.Kilometrage.ToString(), vehDispo});
                item1.Tag = item.Value.NoSerie;
                lstVehicule.Items.Add(item1);
            }
        }

        private void loadLstLocation()
        {
            lstLocation.Items.Clear();
            Client client = CatalogueClient.getInstance().getClient(noPermis);
            Dictionary<int, SessionLocation> locationClient = client.getMapLocation();

            foreach (KeyValuePair<int, SessionLocation> sessionLoc in locationClient)
            {
                Location loc = sessionLoc.Value.getLocation();
                Vehicule veh = sessionLoc.Value.getVehicule();

                ListViewItem item1 = new ListViewItem(new string[] {veh.Immatriculation, veh.Marque, veh.Modele,
                    String.Format("{0:MM/dd/yyyy}", loc.DateDebut) , String.Format("{0:MM/dd/yyyy}", loc.DateFin),
                    loc.Frais.ToString()});

                item1.Tag = loc.IdLocation;
                lstLocation.Items.Add(item1);
            }
        }

        private void lstClient_Click(object sender, EventArgs e)
        {
            var selectedItems = lstClient.SelectedItems;
            foreach (ListViewItem selectedItem in selectedItems)
            {
                noPermis = (String)selectedItem.Tag;
            }

            btnPaiement.Enabled = enableBtnPaiement();
            loadLstLocation();
        }

        private void lstVehicule_Click(object sender, EventArgs e)
        {
            var selectedItems = lstVehicule.SelectedItems;
            foreach (ListViewItem selectedItem in selectedItems)
            {
                noVehicule = (String)selectedItem.Tag;
            }
            setLblDisponible();
            btnPaiement.Enabled = enableBtnPaiement();

            if (sessionLocation.getVehicule(noVehicule).Disponibilite.getDisponibilite())
                btnPaiement.Enabled = true;
        }

        private Boolean enableBtnPaiement()
        {
            if( noPermis.Equals("") || noVehicule.Equals("") )
                return false;

            if (!sessionLocation.getAllVehicule().getVehicule(noVehicule).Disponibilite.getDisponibilite())
                return false;
            
            return true;
        }

        private void setLblDisponible()
        {
            if (sessionLocation.getAllVehicule().getVehicule(noVehicule).Disponibilite.getDisponibilite())
            {
                lblDisponibilite.Text = "Disponible";
                lblDisponibilite.ForeColor = Color.Green;
            }
            else
            {
                lblDisponibilite.Text = "Non disponible";
                lblDisponibilite.ForeColor = Color.Red;
            }
        }

        private void btnEnregistrer_Click(object sender, EventArgs e)
        {
            if (!sessionLocation.getAllVehicule().getVehicule(noVehicule).Disponibilite.getDisponibilite())
            {
                lblDisponibilite.Text = "Non disponible";
                lblDisponibilite.ForeColor = Color.Red;
            }
            else
            {
                sessionLocation.creerLocation(DateTime.Today, dtpFin.Value, 200);
                sessionLocation.getAllVehicule().getVehicule(noVehicule).Disponibilite.EnLocation = true;
                sessionLocation.sauvegarderLocation(noVehicule, noPermis, paiement);
                setLblDisponible();
                btnEnregistrer.Enabled = false;
                loadLstVehicule();
                loadLstLocation();
                lblPaiement.Text = "Sauvegarde effectuée avec succès";
            }
        }

        private Boolean sauvegarderPaiement()
        {
            double prixLocation = 0, depotGarantie = 0, assurance = 0;
            Boolean error = false;

            if (sessionLocation.isNumeric(txtPrix.Text))
                prixLocation = Double.Parse(txtPrix.Text);
            else
                error = true;

            if (sessionLocation.isNumeric(txtDepotGarantie.Text))
                depotGarantie = Double.Parse(txtDepotGarantie.Text);
            else
                error = true;

            if (sessionLocation.isNumeric(txtAssurance.Text))
                assurance = Double.Parse(txtAssurance.Text);
            else
                error = true;

            // Effectuer le paiement
            paiement = new Facture();
            paiement.factureLocation(prixLocation, depotGarantie, assurance);
            paiement.setTotal();

            // Remplir les textBoxs
            txtTps.Text = paiement.Tps.ToString();
            txtTvq.Text = paiement.Tvq.ToString();
            txtSousTotal.Text = paiement.SousTotal.ToString();
            txtTotal.Text = paiement.Total.ToString();

            return error;
        }

        private void btnPaiement_Click(object sender, EventArgs e)
        {
            Boolean error = sauvegarderPaiement();

            if (error)
            {
                lblPaiement.Text = "Vos données doivent contenir des nombres.";
                lblPaiement.ForeColor = Color.Red;
                btnEnregistrer.Enabled = false;
            }
            else
            {
                lblPaiement.Text = "Paiement effectué! Procédez à la sauvegarde.";
                lblPaiement.ForeColor = Color.Green;
                btnPaiement.Enabled = false;
                btnEnregistrer.Enabled = true;
            }
        }

        private void txtPrix_TextChanged(object sender, EventArgs e)
        {
            sauvegarderPaiement();
        }

        private void txtDepotGarantie_TextChanged(object sender, EventArgs e) 
        {
            sauvegarderPaiement();
        }

        private void txtAssurance_TextChanged(object sender, EventArgs e)
        {
            sauvegarderPaiement();
        }

    }
}
